import type { RematchRootState, RematchDispatch } from '@rematch/core'
import { init } from '@rematch/core'
import type { ExtraModelsFromLoading } from '@rematch/loading'
import loadingPlugin from '@rematch/loading'
import type { RootModel } from './rootModel'
import models from './loader'

type FullModel = ExtraModelsFromLoading<RootModel>

export interface ILoadingPlugin {
  loading: {
    models: RematchRootState<RootModel>
    effects: RootDispatch
  }
}

const configureStore = () => {
  const store = init<RootModel, FullModel>({
    plugins: [loadingPlugin()],
    models: models as RootModel,
  })
  return store
}

const store = configureStore()

export type Store = RematchRootState<RootModel> & ILoadingPlugin
export type RootState = RematchRootState<RootModel, FullModel>
export type RootDispatch = RematchDispatch<RootModel>

export default store
